#coding=utf-8  
from _ctypes import byref
from ctypes import windll
import ctypes
from ctypes.test.test_as_parameter import POINT
import time

import pyautogui
import win32api
import win32con
import win32gui  


__author__ = 'Eruca'  
   
__doc__ = ''''' 
pythonwin中win32gui的用法 
本文件演如何使用win32gui来遍历系统中所有的顶层窗口， 
并遍历所有顶层窗口中的子窗口 
'''  
   



# 注册DD DLL，64位python用64位，32位用32位，具体看DD说明文件。
# 测试用免安装版。
# 用哪个就调用哪个的dll文件。
key_dll = windll.LoadLibrary('KeyCall.64.dll')

def 发送字符串(PChar):
    key_dll.KeySendChar(PChar);


def 键盘单击(code):
    # 进行一组按键。
    key_dll.KeyDownUp(0, code);
    time.sleep(0.05)
    
def 键盘组合键(code1,code2):
    # 进行一组按键。
    key_dll.KeyDownUp(code1+code2)

def 鼠标左键单击(s):
    key_dll.MouseClick(1)

def 鼠标右键单击(s):
    key_dll.MouseClick(2)
def 鼠标绝对移动(x,y):
    i=0;
    while ((pyautogui.position()[0]!=x or pyautogui.position()[1]!=y) and i<10) :
        key_dll.MouseMoveTo(0,x,y)
        i=i+1
    if i>=9:
        print("调用9次")
        
def 鼠标绝对移动后左键单击(x,y):
    鼠标绝对移动(x,y);
    time.sleep(1)
    鼠标左键单击(0.1)       
def 鼠标绝对移动后右键单击(x,y):
    鼠标绝对移动(x,y);
    time.sleep(1)
    鼠标右键单击(0.1)  

def gbk2utf8(s):  
    return s.decode('gbk').encode('utf-8')  
   
def 显示窗口句柄相关信息(hWnd):  
    ''''' 
    显示窗口的属性 
    :return: 
    '''  
    if not hWnd:  
        return  
   
    #中文系统默认title是gb2312的编码  
    title = win32gui.GetWindowText(hWnd)  
    clsname = win32gui.GetClassName(hWnd)  
   
    print ('窗口句柄:%s ' % (hWnd)  )
    print ('窗口标题:%s' % (title) ) 
    print ('窗口类名:%s' % (clsname) ) 
    print (''  )
def 显示子窗口窗口句柄相关信息(hWndList):
    for h in hWndList: 
        显示窗口句柄相关信息(h)
def 过滤顶级窗口返回梦幻相关窗口集合(hWndList):
    #获取句柄
    hwnd = win32gui.FindWindow(None, "梦幻西游 ONLINE")  
    hWndListCoy=[]
    for h in hWndList: 
        if win32gui.GetClassName(h)==win32gui.GetClassName(hwnd) :
            显示窗口句柄相关信息(h)
            hWndListCoy.append(h)
    return hWndListCoy 
def 取得梦幻窗口列表():  
    ''''' 
    演示如何列出所有的顶级窗口 
    :return: 
    '''  
    hWndList = []  
    win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList)  
    hWndList=过滤顶级窗口返回梦幻相关窗口集合(hWndList)  
   
    return hWndList  

def 根据父窗口获取子窗口(parent):        
    '''     
    获得parent的所有子窗口句柄
     返回子窗口句柄列表
     '''     
    if not parent:         
        return      
    hwndChildList = []     
    win32gui.EnumChildWindows(parent, lambda hwnd, param: param.append(hwnd),  hwndChildList)  
    显示子窗口窗口句柄相关信息(hwndChildList)        
    return hwndChildList 
###################################
def 打开梦幻exe():
    鼠标绝对移动后左键单击(95, 1055)
    time.sleep(1)
    for i in "my.exe":
        发送字符串(i)
    time.sleep(3)
    键盘单击("enter");
    
    time.sleep(5)
    
    
    
    print("回车结束")
    
    鼠标绝对移动后左键单击(788, 683)
    
    time.sleep(5)
    
    print("点击确认结束")
def 点击登录():
    
    hwndChildList=取得梦幻窗口列表()
    hwnd = win32gui.FindWindow(None, "梦幻西游 ONLINE")  
    win32gui.SetForegroundWindow(hwnd)
    idx=hwndChildList.index(hwnd)
    #获取窗口左上角和右下角坐标
    #left, top, right, bottom = win32gui.GetWindowRect(hwndChildList[3])
    childList=根据父窗口获取子窗口(hwndChildList[idx-1])
    进入游戏=win32gui.FindWindowEx(hwndChildList[idx-1],None,"Button","进入游戏")
    print(进入游戏)
    win32gui.SetForegroundWindow(进入游戏)
    left, top, right, bottom = win32gui.GetWindowRect(进入游戏)
    print(win32gui.GetWindowRect(进入游戏))
    time.sleep(3)
    鼠标绝对移动后左键单击(right-23,bottom-30);
def 点击下一步():
    hwnd = win32gui.FindWindow(None, "梦幻西游 ONLINE")  
    win32gui.SetForegroundWindow(hwnd)
    ###################################
    hwin=win32gui.FindWindow('MHXYMainFrame', "梦幻西游 ONLINE") ;
    print(hwin)
    win32gui.SetForegroundWindow(hwin)
    List=根据父窗口获取子窗口(hwin)
    print(win32gui.GetWindowRect(List[1]))
    left, top, right, bottom =win32gui.GetWindowRect(List[1])
    鼠标绝对移动后左键单击(right-115,bottom-60)  
    
def main():
    打开梦幻exe()
key_dll.SetWaitTick(500,50,30,100)
print(pyautogui.position())
print(key_dll.MouseMoveTo(0,905,505))
print(pyautogui.position())
print(key_dll.MouseMoveTo(1,900,500))
print(pyautogui.position())
print(key_dll.MouseMoveTo(0,900,500))